﻿using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Runtime.Tracking;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using YakShaver.Contract;
using YakShaver.YakServer;
using System.Reflection;
using System.Windows.Forms;
using System.Net.Mail;
namespace YakShaver.Tests
{
    
    
    /// <summary>
    ///This is a test class for IWorkItemServiceTest and is intended
    ///to contain all IWorkItemServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class WorkItemServiceTests
    {
        

        protected static string dbName;
        protected static string trackingDbName;
        protected static string sqlTrackingConnectionString;
        protected static string sqlPeristenceConnectionString;

        protected static string connStr;
        private static WorkflowRuntime workflowRuntime;
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        private static ServiceHost _wcfServiceHost = null;

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {

            sqlTrackingConnectionString = "Data Source=.\\SQLExpress;Initial Catalog=WFTracking;integrated security=true;";
            sqlPeristenceConnectionString = "Data Source=.\\SQLExpress;Initial Catalog=WFPersistence;integrated security=true;";
            

            // recreate the tracking db

           
            // recreate the persistance db

                // initialize the runtime
            workflowRuntime = new WorkflowRuntime();
            ExternalDataExchangeService dataExchangeService = new ExternalDataExchangeService();
            workflowRuntime.AddService(dataExchangeService);

            SqlTrackingService sqlTrackingService = new SqlTrackingService(sqlTrackingConnectionString);
            SqlWorkflowPersistenceService sqlPersistanceService = new SqlWorkflowPersistenceService(sqlPeristenceConnectionString);

            workflowRuntime.AddService(sqlTrackingService);
            workflowRuntime.AddService(sqlPersistanceService);

            workflowRuntime.StartRuntime();

          
       
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            //_wcfServiceHost.Close();

            System.Collections.ObjectModel.ReadOnlyCollection<WorkflowInstance> workflowInstances =
             workflowRuntime.GetLoadedWorkflows();

            // workflowInstances[0].Unload();

            workflowRuntime.StopRuntime();

        //    TakeoutExpress.Support.Linq.TakeoutExpress2008DataContext dc = new TakeoutExpress2008DataContext(connStr);
         //   dc.Connection.Close();
           // if (dc.DatabaseExists())
           //     dc.DeleteDatabase();
        }
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            System.IO.DirectoryInfo testLogDir = new System.IO.DirectoryInfo(TestContext.TestLogsDir);
            foreach (System.IO.FileInfo file in testLogDir.GetFiles("*.eml"))
            {
                TestContext.AddResultFile(file.FullName);
                
            }
        }
        //
        #endregion


       /* internal virtual IWorkItemService CreateIWorkItemService()
        {
            // TODO: Instantiate an appropriate concrete class.
            IWorkItemService target = null;
            return target;
        }
        */

        

       

     

        [TestMethod()]
        public void TestIAnalysisService()
        {
            MockRepository mocks = new MockRepository();

            IAnalysisService mockAnalysisService = mocks.CreateMock<IAnalysisService>();
            IWorkItemDataContract dataContract = mocks.Stub<IWorkItemDataContract>();
            YakShaverWorkItem mockWorkItem = mocks.Stub<YakShaverWorkItem>();

            mocks.Record();

            dataContract.Description = "Test Description";
            dataContract.PageId = "/home/test.aspx";
            dataContract.WorkItemImpactType =  WorkItemImpactType.Unknown;
            dataContract.WorkItemType = WorkItemType.Unknown;

            mockWorkItem.Component = string.Empty;
            mockWorkItem.Description = "Test Description";
            mockWorkItem.Status = WorkItemSubmissionStatus.NeedIntervention;
            mockWorkItem.WorkItemImpactType = WorkItemImpactType.Medium;
            mockWorkItem.WorkItemType = WorkItemType.Task;

            Expect.Call(mockAnalysisService.AnalyzeSubmittedWorkItem(ref dataContract)).Return(mockWorkItem);

            mocks.ReplayAll();

            YakShaverWorkItem returnValue = mockAnalysisService.AnalyzeSubmittedWorkItem(ref dataContract);

            Assert.IsTrue(mockWorkItem.Component == string.Empty);
            Assert.IsTrue(mockWorkItem.Status == WorkItemSubmissionStatus.NeedIntervention);
            Assert.IsTrue(mockWorkItem.Description != string.Empty);

            mocks.VerifyAll();
            
            
        }

        /// <summary>
        ///A test for SubmitWorkItem
        ///</summary>
     
        [TestMethod()]
        public void TestWorkflowCreation()
        {

            MockRepository mocks = new MockRepository();
           // Gener
           // workflowRuntime.CreateWorkflow(typeof(WorkItemEntryWorkflow), 
        }
    }
}
